﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// EmpManager 的摘要描述
/// </summary>
public class EmpManager
{
	public EmpManager()
	{
		//
		// TODO: 在此加入建構函式的程式碼
		//

        var db = new DB();
        
        UserName = HttpContext.Current.User.Identity.Name;
        EmpID = db.ExecuteScalar<string>(@"SELECT EmpID FROM EmpData WHERE NTAccount = @NTAccount "
            , new DBParameter("NTAccount", UserName));

        // EmpID NULL無法作為 sql query 參數
        if (string.IsNullOrEmpty(EmpID))
        {
            EmpID = string.Empty;
        }
	}

    public EmpManager(string empID)
    {
        this.EmpID = empID;
    }

    private DataRow _Data;

    public string this[string colName]
    {
        get
        {
            if (UserName == string.Empty)
            {
                return string.Empty;
            }

            if (_Data == null)
            {
                var db = new DB();

                _Data = db.ExecuteRow(@"SELECT e.*, b.BranchID, c.CompID FROM EmpData e
                                        INNER JOIN OrgData o ON e.OrgID = o.OrgID
                                        INNER JOIN BranchData b ON o.BranchID = b.BranchID
                                        INNER JOIN CompData c ON b.CompID = c.CompID
                                        WHERE EmpID = @EmpID", new DBParameter("EmpID", EmpID));

                EmpID = _Data["EmpID"].ToString();
            }

            return _Data[colName].ToString();
        }
    }

    public string UserName
    {
        get;
        private set;
    }

    public string EmpID
    {
        get;
        private set;
    }

    public static EmpManager Current
    {
        get
        {
            if (HttpContext.Current.Items["EmpManager"] == null)
            {
                HttpContext.Current.Items["EmpManager"] = new EmpManager();
            }

            return HttpContext.Current.Items["EmpManager"] as EmpManager;
        }
    }
}